package com.ww.grtsmanager.service.impl;

import com.ww.grtsmanager.bean.Chapter;
import com.ww.grtsmanager.mapper.ChapterMapper;
import com.ww.grtsmanager.mapper.VideoMapper;
import com.ww.grtsmanager.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Override
    public void addChapter(Chapter chapter) {
        Date date = new Date();
        chapter.setGmtCreated(date);
        chapter.setGmtModified(date);
        chapter.setSort("x");
        int i = chapterMapper.addChapter(chapter);
        if (i==0){
            throw new RuntimeException("添加章节失败");
        }
    }

    @Override
    public void deleteChapter(Integer id) {
        videoMapper.deleteVideoByChapterId(id);
        int i = chapterMapper.deleteChapter(id);
        if (i==0){
            throw new RuntimeException("该章节已被删除，请刷新页面");
        }
    }

    @Override
    public void sort(Integer chapterId, String sort) {
        int i = chapterMapper.changeSort(chapterId,sort);
        if (i==0){
            throw new RuntimeException("修改排序失败");
        }
    }
}
